package com.example.aigc_education.integration.processor.v2;

import com.example.aigc_education.common.BusinessException;
import com.example.aigc_education.integration.emuns.CozeWorkflowTypeV2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * Coze处理器工厂
 * 负责创建和管理所有处理器实例
 */
@Component
public class CozeProcessorFactory {

//    @Resource
//    private List<CozeSyncProcessor<?, ?>> syncProcessors;

    @Resource
    private List<CozeAsyncProcessor<?>> asyncProcessors;

    /**
     * 获取同步处理器
     * @param workflow 工作流类型
     * @return 同步处理器
     */
//    @SuppressWarnings("unchecked")
//    public <T, R> CozeSyncProcessor<T, R> getSyncProcessor(CozeWorkflowTypeV2 workflow) {
//        return (CozeSyncProcessor<T, R>) syncProcessors.stream()
//                .filter(p -> p.workflowId().equals(workflow.getWorkflowId()))
//                .findFirst()
//                .orElseThrow(() -> new BusinessException("未找到对应的同步处理器: " + workflow.getName()));
//    }

    /**
     * 获取异步处理器
     * @param workflow 工作流类型
     * @return 异步处理器
     */
    @SuppressWarnings("unchecked")
    public <T> CozeAsyncProcessor<T> getAsyncProcessor(CozeWorkflowTypeV2 workflow) {
        return (CozeAsyncProcessor<T>) asyncProcessors.stream()
                .filter(p -> p.workflowType() == workflow)
                .findFirst()
                .orElseThrow(() -> new BusinessException("未找到对应的异步处理器: " + workflow.getName()));
    }
}
